#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: select_xibei_4_province.py
@time: 2018/2/27 15:28
"""

import sys

sys.path.append('..')
sys.path.append('../..')
from common import util

from common.mongo import MongDb
from config.mongo_conf import MONGO_DB_SOURCE

# 日志模块
from logger import Logger

log = Logger('select_xibei_4_province.log').get_logger()

company_data_db = MongDb(MONGO_DB_SOURCE['host'], MONGO_DB_SOURCE['port'], MONGO_DB_SOURCE['db'],
                         MONGO_DB_SOURCE['username'],
                         MONGO_DB_SOURCE['password'], log=log)

APP_DATA_DB_SOURCE = {
    'host': '172.16.215.16',
    'port': 40042,
    'db': 'app_data',
    'username': 'work',
    'password': 'haizhi'
}
app_data_db = MongDb(APP_DATA_DB_SOURCE['host'], APP_DATA_DB_SOURCE['port'], APP_DATA_DB_SOURCE['db'],
                     APP_DATA_DB_SOURCE['username'],
                     APP_DATA_DB_SOURCE['password'], log=log)

NEED_PROVINCE_SET = {
    u'青海', u'新疆', u'宁夏', u'陕西'
}


# 获得省份信息
def get_province(province):
    for origin_province in NEED_PROVINCE_SET:
        if origin_province in province:
            return origin_province

    return None


def main():
    # 需要提取数据的表
    source_table = 'enterprise_data_gov'
    target_table = 'lanzhou_import_company_list'

    count = 0
    total = 0
    result_list = []
    for item in app_data_db.traverse_batch_field(source_table, {},
                                                 ['company', 'registered_capital',
                                                  'province', 'business_status']):
        total += 1
        province = item.get('province')
        if province is None:
            continue

        # 判断是否是重点省份
        cal_province = get_province(province)
        if cal_province is None:
            continue

        business_status = item.get('business_status')
        if business_status is None:
            continue

        # 去除注销吊销企业
        if u'吊销' in business_status or u'注销' in business_status:
            continue

        company = item.get('company')
        registered_capital = item.get('registered_capital')

        try:
            registered_capital = float(registered_capital)
        except:
            continue

        # 没有注册资本的数据不存储
        if registered_capital <= 0.0:
            continue

        result_list.append({
            '_id': company,
            'province': cal_province,
            'registered_capital': registered_capital,
            '_in_time': util.get_now_time(),
        })
        count += 1
        if len(result_list) >= 500:
            company_data_db.insert_batch_data(target_table, result_list)
            del result_list[:]

        if count % 1000 == 0:
            log.info("当前进度: total = {} count = {}".format(total, count))

    if len(result_list) > 0:
        company_data_db.insert_batch_data(target_table, result_list)
        del result_list[:]


if __name__ == '__main__':
    main()
